package com.miaosha.service.impl;

import com.miaosha.dao.UserInformationDOMapper;
import com.miaosha.dao.UserPasswordDOMapper;
import com.miaosha.dataobject.UserInformationDO;
import com.miaosha.dataobject.UserPasswordDO;
import com.miaosha.error.EnumError;
import com.miaosha.error.ErrorException;
import com.miaosha.model.UserInformationModel;
import com.miaosha.service.UserService;
import com.miaosha.validator.ValidationResult;
import com.miaosha.validator.ValidatorImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserInformationDOMapper userInformationDOMapper;

    @Autowired
    private UserPasswordDOMapper userPasswordDOMapper;

    @Autowired
    private ValidatorImpl validator;

    @Override
    public UserInformationModel  getUserById(Integer id) {
        UserInformationDO userInformationDo = userInformationDOMapper.selectByPrimaryKey(id);
        if (userInformationDo == null) return null;
        UserPasswordDO userPasswordDO = userPasswordDOMapper.selectByUserId(userInformationDo.getId());
      return convertFromUserInformationDo(userInformationDo, userPasswordDO);
    }

    @Override
    @Transactional
    public void register(UserInformationModel userInformationModel) throws ErrorException{
        if (userInformationModel == null) {
            throw new ErrorException(EnumError.PARAMETER_VALIDATION_ERROR);
        }
//        if (StringUtils.isEmpty(userInformationModel.getName())
//                || userInformationModel.getGender() == null
//                || userInformationModel.getAge() == null
//                || StringUtils.isEmpty(userInformationModel.getMobile())){
//            throw new ErrorException(EnumError.PARAMETER_VALIDATION_ERROR);
//        }
        ValidationResult validationResult = validator.validation(userInformationModel);
        if (validationResult.isHasError()){
            throw new ErrorException(EnumError.PARAMETER_VALIDATION_ERROR, validationResult.getErrorMessage());
        }

        // model转dataobject
        UserInformationDO userInformationDO = convertFormUserInformationModel(userInformationModel);
        try {
            userInformationDOMapper.insertSelective(userInformationDO);
        } catch (DuplicateKeyException e) {
            throw new ErrorException(EnumError.PARAMETER_VALIDATION_ERROR,"手机号码已经被注册");
        }

        // 设置userId
        userInformationModel.setId(userInformationDO.getId());

        if (StringUtils.isEmpty(userInformationModel.getEncryptPassword())){
            throw new ErrorException(EnumError.PARAMETER_VALIDATION_ERROR, "密码不能为空");
        }
        UserPasswordDO userPasswordDO = convertFormUserPasswordModel(userInformationModel);
        userPasswordDOMapper.insertSelective(userPasswordDO);
    }

    @Override
    public UserInformationModel validateLogin(String mobile, String encryptPassword) throws ErrorException {
        // 通过手机号码获取用户信息
        UserInformationDO userInformationDO = userInformationDOMapper.selectByMobile(mobile);
        if (userInformationDO == null){
            throw  new ErrorException(EnumError.USER_LOGIN_FAILURE);
        }

        UserPasswordDO userPasswordDO = userPasswordDOMapper.selectByUserId(userInformationDO.getId());
        if (!StringUtils.equals(encryptPassword, userPasswordDO.getEncryptPassword())){
            throw  new  ErrorException(EnumError.USER_LOGIN_FAILURE);
        }
        // 生成userModel返回
        UserInformationModel userInformationModel = convertFromUserInformationDo(userInformationDO, userPasswordDO);
        return userInformationModel;

    }

    private UserPasswordDO convertFormUserPasswordModel(UserInformationModel useInformationModel){
        if (useInformationModel == null) return null;
        UserPasswordDO userPasswordDO = new UserPasswordDO();
        userPasswordDO.setEncryptPassword(useInformationModel.getEncryptPassword());
        userPasswordDO.setUserId(useInformationModel.getId());
        return userPasswordDO;
    }

    private UserInformationDO convertFormUserInformationModel(UserInformationModel useInformationModel){
        if (useInformationModel == null) return null;
        UserInformationDO userInformationDO = new UserInformationDO();
        BeanUtils.copyProperties(useInformationModel, userInformationDO);
        return userInformationDO;
    }

    private UserInformationModel convertFromUserInformationDo(UserInformationDO userInformationDO, UserPasswordDO userPasswordDO){
        if (userInformationDO == null) return null;
        UserInformationModel userInformationModel = new UserInformationModel();
        BeanUtils.copyProperties(userInformationDO, userInformationModel);
        if (userPasswordDO != null) {
            userInformationModel.setEncryptPassword(userPasswordDO.getEncryptPassword());
        }
        return userInformationModel;
    }
}
